var connector = new LineConnector({
hasPushApi: false, //you need to pay push api >.,<
channelId: config.channelId,
channelSecret: config.channelSecret,
channelAccessToken: config.channelAccessToken
});
server.post('/line', connector.listen());
var bot = new builder.UniversalBot(connector)
bot.dialog("/", s => {
s.beginDialog("start")
})
bot.on('conversationUpdate', function (message) {
switch (message.text) {
case 'follow':
break;
case 'unfollow':
break;
case 'join':
bot.beginDialog(message.address, "start")
break;
case 'leave':
break;
}
console.log("conversationUpdate")
});
bot.dialog("start", s => {
s.send(new builder.Message(s).addAttachment(new Sticker(s, 1, 2)));
s.send("遊戲開始")
s.beginDialog("ask");
})
bot.dialog("ask", [
(s, args) => {
let q: {
"q": string,
"options": Array<string>,
"a": string
};
if (args && args.q) {
q = args.q
} else {
q = q_list[Math.floor(Math.random() * (q_list.length - 1))];
}
s.dialogData.q = q;
//獲得題目,或是未答對上一題,再打下去。
builder.Prompts.choice(s, new builder.Message(s)
.text(q.q)
.suggestedActions(
builder.SuggestedActions.create(
s, q.options.map(o => {
return builder.CardAction.imBack(s, o, o)
})
)),
q.options
);
},
(s, r) => {
s.dialogData.answers = [];
if (r.response.entity === s.dialogData.q.a) {
s.send(`${s.message.from.name} 答對了! 加一分!`)
s.send(new builder.Message(s).addAttachment(new Sticker(s, 1, 2)));
s.dialogData.q = null
} else {
s.send(`${s.message.from.name} 答錯了! 扣一分!`)
s.send(new builder.Message(s).addAttachment(new Sticker(s, 1, 3)));
}
s.replaceDialog("ask", { q: s.dialogData.q })
}
])
結果這個比較麻煩的部份,是去爬題庫。哈